package com.atguigu.pojo;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @program: IDEAcode
 * @description:
 * @author: Mr.zjx
 * create: 2021-07-26-21-09
 **/
public class test {
    @Test
    public void testConstructor() throws Exception {
        Class clazz = Class.forName("com.atguigu.pojo.Person");
        //获取无参数的构造方法getConstructor（不写参数）
        Constructor con = clazz.getConstructor();
        System.out.println(con);
        //运行构造方法，创建对象
        Object obj = con.newInstance();
        System.out.println("obj = " + obj);
    }

    @Test
    public void testConstructor02() throws Exception{
        Class clazz= Class.forName("com.atguigu.pojo.Person");
        Constructor con = clazz.getConstructor(String.class, int.class);
        System.out.println("con = " + con);
        Object obj = con.newInstance("zhangsan", 30);
        System.out.println("obj = " + obj);
    }

    @Test
    public void testMethod() throws Exception{
        Class clazz = Class.forName("com.atguigu.pojo.Person");
        //newInstance：生成无参构造方法，返回Object
        Object obj = clazz.newInstance();
        //反射获取类中的指定方法
        Method method = clazz.getMethod("eat");
        System.out.println("method = " + method);
        //invoke运行方法
        method.invoke(obj);
    }

    @Test
    public void testMethod2() throws Exception{
        Class clazz = Class.forName("com.atguigu.pojo.Person");
        Object obj = clazz.newInstance();
        Method method = clazz.getMethod("sleep", String.class, int.class, double.class);
        method.invoke(obj,"字符串",10,10.5);
    }

    @Test
    public void testInterface() throws Exception{
        Class clazz = Class.forName("com.atguigu.pojo.Person");
        Class[] interfaces = clazz.getInterfaces();
        for (Class c :interfaces){
            System.out.println(c);
        }
    }
}
